public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val=val;
        }
    }
    public TreeNode root;

    /**
     * 二叉搜索树中进行元素的插入
     * @param val
     * @return
     */
    public boolean insert(int val){
        if (root==null){
            root=new TreeNode(val);
            return true;
        }
        TreeNode cur=root;
        TreeNode parent=null;//记录cur前一个节点的地址
       TreeNode treeNode=new TreeNode(val);
        while (cur!=null){
            if (cur.val<val){
                parent=cur;
                cur=cur.right;
            }else if(cur.val>val){
                parent=cur;
                cur=cur.left;
            }else {
                return false; //插入的值与该结点的值相等，插入失败
            }
        }
        if (parent.val>val){
            parent.left=treeNode;
        }
        if (parent.val<val){
            parent.right=treeNode;
        }
        return true;
    }

    /**
     * 二叉搜索树中进行元素的查找
     * @param key
     * @return
     */
    public TreeNode search(int key){
        if (root==null){
            return null;
        }
        TreeNode cur=root;
        while (cur!=null){
            if (cur.val<key){
                cur=cur.right;
            } else if (cur.val==key) {
                return cur;
            }
            else {
                cur=cur.left;
            }
        }
        return null;
    }

    /**
     * 二叉搜索树中进行元素的删除
     * @param val
     */
    public void remove(int val){
        if (root==null){
            return;
        }
        //找到要删除的那个元素
        TreeNode cur=root;
        TreeNode parent=null;
        while (cur!=null){
            if (cur.val<val){
                parent=cur;
                cur=cur.right;
            } else if (cur.val==val) {
                removeChild(parent,cur);
                return;
            }else {
                parent=cur;
                cur=cur.left;
            }
        }
    }

    private void removeChild(TreeNode parent, TreeNode cur) {
        if (cur.left==null){
            if (cur==root){
                root=cur.right;
            } else if (cur==parent.left) {
                parent.left=cur.right;
            }else {
                parent.right=cur.right;
            }
        } else if (cur.right==null) {
          if (cur==root){
              root=cur.left;
          } else if (cur==parent.left) {
              parent.left=cur.left;
          }else {
              parent.right=cur.left;
          }
        }else {
        TreeNode target=cur.right;
        TreeNode targetParent=cur;
        while (target.left!=null){
            targetParent=target;
            target=target.left;
        }
        cur.val=target.val;
        if (target==targetParent.left){
            targetParent.left=target.right;
        }if (target==targetParent.right){
                targetParent.right=target.right;
            }
        }
    }
}
